<!-- 小程序用户编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改小程序用户':'添加小程序用户'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      
      <el-form-item label="头像:">
        <uploadImage :limit="1" v-model="form.avatar"></uploadImage>
      </el-form-item>
                        
      <el-form-item
        label="openid:"
        prop="openid">
        <el-input
          :maxlength="20"
          v-model="form.openid"
          placeholder="请输入openid"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="真实姓名:"
        prop="realname">
        <el-input
          :maxlength="20"
          v-model="form.realname"
          placeholder="请输入真实姓名"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="昵称:"
        prop="nickname">
        <el-input
          :maxlength="20"
          v-model="form.nickname"
          placeholder="请输入昵称"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="手机号码:"
        prop="mobile">
        <el-input
          :maxlength="20"
          v-model="form.mobile"
          placeholder="请输入手机号码"
          clearable/>
      </el-form-item>
                      
      <el-form-item label="状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">禁用</el-radio>
                </el-radio-group>
      </el-form-item>
                      
      <el-form-item label="角色:" prop="type">
        <el-select
          clearable
          class="ele-block"
          v-model="form.type"
          placeholder="请选择角色">
                  <el-option label="普通用户" :value="1"/>
                  <el-option label="老师" :value="2"/>
                </el-select>
      </el-form-item>
                              
      <el-form-item label="显示顺序:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入显示顺序"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'WxappuserEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
                  realname: [
          {required: true, message: '请输入真实姓名', trigger: 'blur'}
        ],
                  mobile: [
          {required: true, message: '请输入手机号码', trigger: 'blur'}
        ],
                  
        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
                          
        type: [
          {required: true, message: '请选择角色', trigger: 'blur'}
        ],
                          sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
                  intro: [
          {required: true, message: '请输入个人简介', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    },
    'form.type': function(newVal, oldVal) {
      console.log(`Type changed from ${oldVal} to ${newVal}`);
      if(newVal == 2){
          if(this.form.mobile == '' || this.form.mobile === undefined || this.form.mobile === null){
              this.$message.error('该普通用户还未填写电话号码，无法变成老师角色，请先填写电话号码再修改角色！');
              this.form.type = 1;
          }
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/wxappuser/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
